package io.renren.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * @author PzC.
 * @time 2017年1月6日 下午2:33:30
 * 
 */
public class Md5Util {
	private static Md5Util instance = new Md5Util();

	private Md5Util() {
	}

	public static Md5Util getInstance() {
		if (null == instance) {
			instance = new Md5Util();
		}
		return instance;
	}

	public String getmd5Code(String inStr) {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "";
		}
		byte[] md5Bytes = md5.digest(inStr.getBytes());
		StringBuilder hexValue = new StringBuilder();
		for (byte md5Byte : md5Bytes) {
			int val = ((int) md5Byte) & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString().toUpperCase();
	}

	/**
	 * 加密解密算法 执行一次加密，两次解密
	 */
	public String convertmd5(String inStr) {
		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		return new String(a);
	}
}
